<?php

namespace Modules\Manager\Http\Controllers\Admin;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Models\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Modules\Manager\Entities\Department;
use Modules\Manager\Entities\Manager;
use Spatie\Permission\Models\Role;

class RegisterController extends Controller
{
    use ResponseData;
    public function list(Manager $manager,Request $request){
        $size = $request->input('size',10);
        if($request->filled('search')){
            $data = $manager::query()
                ->where('phone',$request->search)
                ->with('post','department')
                ->orderByDesc('id')->paginate($size);
        } else {
            $data = $manager::query()
                ->with('post','department')
                ->orderByDesc('id')->paginate($size);
        }
        return $this->success($data);
    }
    public function create(Request $request){
        $post = Role::query()->get();
        $department = Department::query()->get();
        return $this->success(compact('post','department'));
    }

    public function store(Manager $manager,Request $request){
        $validated = $this->validator($request->input());
        if($validated->fails()){
            $errors = $validated->errors()->messages();
            $errors['code']=201;
            return $errors;
        }
        $data = $request->all();
        $data['password'] = Hash::make($data['password']);
        $user = $manager->fill($data);
        $user->save();
        if($request->filled('post_id')){
            $post = Role::find($request->post_id);
            //info($post);
            $user->syncRoles($post->name,'');
        }
        return $this->success($user,200);
    }
    public function edit(Manager $manager,Request $request){
        $data = $manager->find($request->id);
        return $this->success($data);
    }
    public function update(Manager $manager,Request $request){
        $data = $manager->find($request->id);
        $data->update($request->all());
        return $this->success('修改成功');
    }
    public function updateopenid(Manager $manager,Request $request){
        $data = $manager->find($request->id);
        $data->openid = $request->openid;
        $data->save();
        return $this->success('修改成功');
    }
    public function destory(Manager $manager,Request $request){
        $data = $manager->find($request->id);
        $data->delete();
    }
    //验证用户密码
    protected function validator(array $data){
        $rules = [
            'phone' => 'required|string|min:11|unique:managers|alpha_dash',
            'password' => 'required|string|min:6|confirmed',
        ];
        $messages = [
            'unique'=>':attribute 手机号码已经存在',
            'required'=>':attribute 为必填项',
            'min'=>':attribute 长度最少 :min ,不符合要求',
            'email'=>':attribute 格式错误',
            'confirmed'=>'两次密码输入不一致'
        ];
        return Validator::make($data,$rules,$messages);
    }
    public function resetPasswd(Request $request,Manager $manager){
        $account = $manager->query()->find($request->id);
        if($account){
           // if (Hash::check($request->password, $account->password)) {
                $account->password = Hash::make($request->password);
                $account->save();
                return $this->success('修改密码成功');
//            }else{
//                return $this->failed('请输入正确的账号密码',201);
//            }
        }else{
            return $this->failed('账号不存在',201);
        }

    }
    public function resetPasswdDz(Request $request,Manager $manager){
        $account = $manager->query()->find($request->id);
        if($account){
           // if (Hash::check($request->password, $account->password)) {
                $account->password = Hash::make($request->password);
                $account->next_change_time = $request->next_change_time;
                $account->save();
                return $this->success('修改密码成功');
//            }else{
//                return $this->failed('请输入正确的账号密码',201);
//            }
        }else{
            return $this->failed('账号不存在',201);
        }

    }
    //为当前用户分配角色
    public function role(Request $request){
        $user = Manager::query()->find($request->id);
        if(!$user){
            return $this->failed('用户不存在',201);
        }
        $name = Role::query()->select('name')->find($request->role_id);
        $user->syncRoles($name->name);
        return $this->success('添加成功');
    }
    //获取用户所有角色
    public function  getRole(Request $request){
        $user = $request->user();
        $roles = $user->getRoleNames();
        $id = Role::query()->whereIn('name',$roles)->pluck('id');
        return $this->success($id);

    }
}
